﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Odyssey.Linq;
using SunTzu.Core.Data;
using SunTzu.Utility;
using Odyssey.Web.Models;


namespace Odyssey.Web.Controllers
{
    public class NewsController : FrontBaseController
    {
        public ActionResult Index()
        {
            ViewBag.NewsClasses = GetTable<tb_subclass>().Where(item => item.isnews == "Y").OrderBy(item => item.DisplayOrder);
            ViewBag.Articles = GetTable<tb_article>().Join(GetTable<tb_subclass>(), a => a.ClassId, sc => sc.Id, (a, sc) => new { a, sc })
                   .Where(it => it.sc.TopClassId == 1 && it.sc.isnews == "Y")
                   .Select(it => it.a)
                   .OrderByDescending(c => c.Id).ToList();
            BindMasterViewData(1);
            return View();
        }

        protected int numPerPage
        {
            get
            {
                // 从 cookie 读入列表行数
                var cookie = System.Web.HttpContext.Current.Request.Cookies["numPerPage"];
                return cookie == null ? 20 : cookie.Value.ParseToInt(20);
            }
        }


        public ActionResult List(int id, int? page)
        {
            BindMasterViewData(1);
            ViewBag.Articles = GetTable<tb_article>()
                   .Where(it => it.ClassId == id)
                   .OrderByDescending(c => c.Id).ToPagedList(page.GetValueOrDefault(), numPerPage);
            ViewBag.SubClassName = CacheHelper<tb_subclass>.GetById(id).GetName();
            return View();
        }
    }
}